<template>
  <div class="conMain" >
    <el-card  class="box-card" >
      <div slot="header" class="topfix">
          <el-form   :inline="true"  :model="params" size="small" class="goodsForm"  label-width="100px">
              <el-form-item label="ID">
                  <el-input clearable v-model="params.s_id"  placeholder="请输入渠道ID" />
              </el-form-item>
              <el-form-item label="渠道名称">
                  <el-input clearable v-model="params.s_name"  placeholder="请输入渠道名称" />
              </el-form-item>

              <el-form-item label="状态">
                  <el-select clearable v-model="params.s_status"  placeholder="请选择">
                   <el-option  :label="item.txt" :value="item.val" v-for="(item,index) in statuslist" />
                  </el-select>
              </el-form-item>
              <el-form-item label=" ">
                <el-button type="primary"  size="small" class="searchBtn" @click="handleSearch" icon='el-icon-search'>搜索</el-button>
                <el-button v-has="'passage/passage/add'"  type="primary" size="small" class="ml50" @click="add" plain><i class="el-icon-plus" /> 添加渠道</el-button>
              </el-form-item>
          </el-form>
          <div style="text-align: right;">
          </div>
      </div>
      <el-table v-loading="loading"  :data="list" style="width: 100%" :empty-text='emtypetxt'>
          <el-table-column prop="id" label="ID" align="center" width="100" />
          <el-table-column prop="name" label="渠道名称" align="center"  />
          <el-table-column prop="describe" label="描述" :show-overflow-tooltip="true" align="center"  />
          <el-table-column prop="uptime" label="查单范围" align="center" >
            <template slot-scope="scope">
              <span v-if="scope.row.uptime === 0">全部</span>
              <span v-else-if="scope.row.uptime === 1">今天</span>
              <span v-else-if="scope.row.uptime === 2">最近7天</span>
              <span v-else-if="scope.row.uptime === 3">最近15天</span>
              <span v-else-if="scope.row.uptime === 4">最近1月</span>
            </template>

          </el-table-column>
          <el-table-column prop="cd_num" label="查单数量(条)" align="center" />
          <el-table-column prop="jgtime" label="查单间隔(s)" align="center" />
          <el-table-column label="状态" align="center">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status === 1" type="success" size="mini" disable-transitions>上线</el-tag>
              <el-tag v-if="scope.row.status === -1" type="info" size="mini" disable-transitions>下线</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="createtime" label="创建时间" align="center" width="200" />
          <el-table-column label="操作" align="center" width="200">
            <template slot-scope="scope" >
                <el-link v-has="'passage/passage/edit'" :underline="false" type="primary" title='编辑' @click="edit(scope.row)">编辑</el-link>
            </template>
          </el-table-column>
      </el-table>
      <div class="pagination">
          <el-pagination
            :current-page="params.page"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="params.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
      </div>
    </el-card>
    <el-dialog :title="sub.id==0?'添加渠道':'编辑渠道'"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    center  width="40%">
      <el-form ref="ruleForm" :model="sub" :rules="rules" label-width="110px">
        <el-form-item label="渠道名称" prop="name">
          <el-input v-model="sub.name" clearable placeholder="请输入渠道名称" />
        </el-form-item>
        <el-form-item label="描述" >
          <el-input v-model="sub.describe" clearable placeholder="请输入描述" />
        </el-form-item>
        <el-row>
            <el-col :span="12">
                <el-form-item label="查单范围"   prop="uptime">
                    <el-select v-model="sub.uptime" clearable placeholder="请选择">
                        <el-option
                          v-for="(item,index) in uptimelist"
                          :key="item.id"
                          :label="item.txt"
                          :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="查单数量"   prop="cd_num">
                  <el-input v-model.number="sub.cd_num" clearable placeholder="请输入每次查单数量">
                  <template slot="append">条</template>
                  </el-input>
                </el-form-item>
            </el-col>
        </el-row>


        <el-form-item label="查单间隔"  prop="jgtime">
          <el-input v-model.number="sub.jgtime" clearable placeholder="请输入查单间隔">
          <template slot="append">秒</template>
          </el-input>
        </el-form-item>

        <div style="margin-bottom: 20px;">
          <el-form-item style="margin-bottom: 6px!important;"   v-for="(item, index) in sub.params"   :label="index==0?'渠道参数':''"  :key="index">
              <div class="wdgroup" style="display: flex;flex-wrap: wrap;justify-content: flex-start;">
                <el-input  v-model="sub.params[index]['key']"  placeholder="参数名称" style="width:42%!important;margin-right: 10px;"></el-input>
                <el-input  v-model="sub.params[index]['value']"   placeholder="参数值" style="width:42%!important;margin-right: 10px;"></el-input>
                <el-button type="danger" v-if="index>0"  plain="" @click.prevent="removeparams(index)" size="mini"  icon="el-icon-delete"></el-button>
              </div>
          </el-form-item>
          <el-form-item label="">
            <el-button  @click="addparams()" size="mini">+追加</el-button>
          </el-form-item>
        </div>



        <el-form-item label="备注" prop="memo">
          <el-input type="textarea" v-model="sub.memo"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status" >
            <el-radio-group v-model="sub.status" >
              <el-radio label="1">上线</el-radio>
              <el-radio label="-1">下线</el-radio>
            </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="subForm('ruleForm')">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import { passage_index,passage_add_post,passage_edit_post } from  '@/api/passage'


export default {
  data() {
    return {
      total:0,
      loading:true,
      emtypetxt:'',
      list: [],
      statuslist:[{val:'1',txt:'上线'},{val:'0',txt:'下线'}],
      params: {
        s_id:'',
        s_name:'',
        s_status:'',
        ss_name:'like',
        page: 1,
        pagesize:10,
      },
      dialogVisible:false,
      uptimelist:[
        {id:'0',txt:'全部'},
        {id:'1',txt:'今天'},
        {id:'2',txt:'最近7天'},
        {id:'3',txt:'最近15天'},
        {id:'4',txt:'最近1个月'},
      ],
      sub:{
        id:'',
        name:'',
        describe:'',
        memo:'',
        uptime:'0',
        jgtime:'7200',
        cd_num:100,
        status:'1',
        params:[
          {key:'',value:''}
        ]
      },
      rules: {
          name: [
              { required: true, message: '渠道名称不能为空', trigger: 'blur' },
          ],
          uptime: [
              { required: true, message: '查单范围不能为空', trigger: 'blur' },
          ],
          cd_num: [
              { required: true, message: '查单数量不能为空', trigger: 'blur' },
          ],
          jgtime: [
              { required: true, message: '查单间隔不能为空', trigger: 'blur' },
          ],
      },

    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.emtypetxt='加载中...';
      this.loading=true;
      passage_index(this.params).then(res => {
        this.list=res.data.list
        this.total=res.data.total
        this.loading = false
        if(this.list.length==0){
          this.emtypetxt='暂无数据'
        }
      })
    },
    handleSearch(){
      this.list=[];
      this.params.page=1;
      this.getList()
    },
    //分页
    handleSizeChange(val) {
        this.params.pagesize = val
        this.getList()
    },
    handleCurrentChange(val) {
        this.params.page = val
        this.getList()
    },
    add(){
      this.sub={
        id:'',
        name:'',
        describe:'',
        memo:'',
        uptime:'0',
        jgtime:'7200',
        cd_num:100,
        status:'1',
        params:[
          {key:'',value:''}
        ]
      },
      this.dialogVisible=true
    },
    edit(row) {
      this.sub={
        id:row.id,
        name:row.name,
        describe:row.describe,
        memo:row.memo,
        uptime:row.uptime+'',
        jgtime:row.jgtime,
        cd_num:row.cd_num,
        status:row.status+'',
        params:row.params_text,
      }
      this.dialogVisible=true
    },

    subForm(formName) {
        if(this.sub.cd_num<10 || this.sub.cd_num>200){
          this.$message.error('查单数量应在10-200之间')
          return
        }
        if(this.sub.jgtime<300){
          this.$message.error('查单间隔不能小于5分钟')
          return
        }
        this.$refs[formName].validate((valid) => {
            if (valid) {
              if(this.sub.id>0){
                passage_edit_post(this.sub).then(res => {
                    this.$message.success(res.msg)
                    this.dialogVisible = false
                    this.getList()
                })
              }else{
                passage_add_post(this.sub).then(res => {
                    this.$message.success(res.msg)
                    this.dialogVisible = false
                    this.getList()
                })
              }
            } else {
              return false;
            }
        });
    },
    addparams() {
        if(this.sub.params.length>5){
          return this.$message.error('最多只能添加6个参数');
        }
        this.sub.params.push({
            "key":'',
            "value": ''
        });

    },
    removeparams(index) {
      this.sub.params.splice(index, 1)
    },


  },
}
</script>

<style lang="scss" scoped>


</style>
